﻿cmake_minimum_required(VERSION 3.5)

set(CMAKE_CXX_STANDARD 17)
project(guild_service_unittest)

if (MSVC)
else()
  set(CMAKE_CXX_COMPILER "g++")
endif()

include_directories(
    ${PROJECT_SOURCE_DIR}/../../../../..
    ${PROJECT_SOURCE_DIR}/../../../../src/include/root
    ${PROJECT_SOURCE_DIR}/../../../../src/include
    ${PROJECT_SOURCE_DIR}/../../../../src/include/guildservicebasic
    ${PROJECT_SOURCE_DIR}/../../../../src/include/guildservicequery
    ${PROJECT_SOURCE_DIR}/../../../../src/include/guildservicewelfare
    ${PROJECT_SOURCE_DIR}/../../../../src/include/guildserviceauth
    ${PROJECT_SOURCE_DIR}/../../../../src/include/guildservicetimer
    ${PROJECT_SOURCE_DIR}/../../../../src/include/guildserviceactivity
    ${PROJECT_SOURCE_DIR}/../../../../src/include/guildservicetask
    ${PROJECT_SOURCE_DIR}/../../../../src/include/guildservicerank
    ${PROJECT_SOURCE_DIR}/../../../../src/include/guildservicebuilding
    ${PROJECT_SOURCE_DIR}/../../../../src/include/guildservicechat
    ${PROJECT_SOURCE_DIR}/../../../../thirdparty/include
    ${PROJECT_SOURCE_DIR}/../../../../../../thirdparty
    ${PROJECT_SOURCE_DIR}/../../../../../../thirdparty/include
    ${PROJECT_SOURCE_DIR}/../../include
    ${PROJECT_SOURCE_DIR}/../../src/guildservicebasic
    ${PROJECT_SOURCE_DIR}/../../src/guildservicequery
    ${PROJECT_SOURCE_DIR}/../../src/guildservicewelfare
    ${PROJECT_SOURCE_DIR}/../../src/guildserviceauth
    ${PROJECT_SOURCE_DIR}/../../src/guildservicetimer
    ${PROJECT_SOURCE_DIR}/../../src/guildserviceactivity
    ${PROJECT_SOURCE_DIR}/../../src/guildservicetask
    ${PROJECT_SOURCE_DIR}/../../src/guildservicerank
    ${PROJECT_SOURCE_DIR}/../../src/guildservicebuilding
    ${PROJECT_SOURCE_DIR}/../../src/guildservicechat

)

if(WIN32)
	message("WIN32")
else()
	message("LINUX")
endif()


aux_source_directory(${PROJECT_SOURCE_DIR}/../../../../../../unittest/framework EXTRA_FILES)

set(SRC_FILES test_guild_service_basic.cpp
    test_guild_service_welfare.cpp
    test_guild_service_building.cpp
)

add_executable(guild_service_unittest ${SRC_FILES} ${EXTRA_FILES})

if (GCOV)
  message("GCOV On")
  set_source_files_properties(${SRC_FILES} PROPERTIES COMPILE_FLAGS --coverage)
endif()

#静态链接库目录
if (MSVC)
	message("WIN32")
foreach(var
  CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
  CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO
  CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
  CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO
)
if(${var} MATCHES "/MD")
  string(REGEX REPLACE "/MD" "/MT" ${var} "${${var}}")
endif()
endforeach()
    set_target_properties(guild_service_unittest PROPERTIES COMPILE_FLAGS "/EHsc /W3")

    SET_TARGET_PROPERTIES(guild_service_unittest PROPERTIES LINK_FLAGS_DEBUG " 
    /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../../../lib/guildservicebasic/Debug/libguildservicebasic.a
    /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../../../lib/guildservicequery/Debug/libguildservicequery.a
    /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../../../lib/guildservicewelfare/Debug/libguildservicewelfare.a
    /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../../../lib/guildserviceauth/Debug/libguildserviceauth.a
    /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../../../lib/guildservicetimer/Debug/libguildservicetimer.a
    /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../../../lib/guildserviceactivity/Debug/libguildserviceactivity.a
    /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../../../lib/guildservicebuilding/Debug/libguildservicebuilding.a
    /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../../../lib/guildservicechat/Debug/libguildservicechat.a
    /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../../../lib/guildservicetask/Debug/libguildservicetask.a
    /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../../../lib/guildservicerank/Debug/libguildservicerank.a
    ")
    SET_TARGET_PROPERTIES(guild_service_unittest PROPERTIES LINK_FLAGS_RELEASE " 
    /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../../../lib/guildservicebasic/Release/libguildservicebasic.a 
    /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../../../lib/guildservicequery/Release/libguildservicequery.a
    /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../../../lib/guildservicewelfare/Release/libguildservicewelfare.a
    /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../../../lib/guildserviceauth/Release/libguildserviceauth.a
    /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../../../lib/guildservicetimer/Debug/libguildservicetimer.a
    /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../../../lib/guildserviceactivity/Release/libguildserviceactivity.a
    /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../../../lib/guildservicebuilding/Release/libguildservicebuilding.a
    /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../../../lib/guildservicechat/Release/libguildservicechat.a
    /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../../../lib/guildservicetask/Release/libguildservicetask.a
    /WHOLEARCHIVE:${PROJECT_SOURCE_DIR}/../../../../lib/guildservicerank/Release/libguildservicerank.a
    ")

    target_link_libraries(guild_service_unittest
        debug ${PROJECT_SOURCE_DIR}/../../../../../../win-proj/x64/Debug/service-box.lib optimized ${PROJECT_SOURCE_DIR}/../../../../../../win-proj/x64/Release/service-box.lib
        debug ${PROJECT_SOURCE_DIR}/../../../../lib/root/Debug/librpc.a optimized ${PROJECT_SOURCE_DIR}/../../../../lib/root/Release/librpc.a
        ${PROJECT_SOURCE_DIR}/../../../../thirdparty/lib/windows/debug/libprotobufd.lib
        ${PROJECT_SOURCE_DIR}/../../../../thirdparty/lib/windows/release/libprotobuf.lib
        ${PROJECT_SOURCE_DIR}/../../../../../../thirdparty/lib/win/Debug/zookeeper.lib
        ${PROJECT_SOURCE_DIR}/../../../../../../thirdparty/lib/win/Debug/hashtable.lib
        ${PROJECT_SOURCE_DIR}/../../../../../../thirdparty/lib/win/Debug/hiredis.lib
        ${PROJECT_SOURCE_DIR}/../../../../../../thirdparty/lib/win/Debug/Win32_Interop.lib
        ${PROJECT_SOURCE_DIR}/../../../../../../thirdparty/lib/win/Debug/libcurl.a
        ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildservicebasic/build/Debug/lib/libguild_service_basic.a
        ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildservicequery/build/Debug/lib/libguild_service_query.a
        ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildservicewelfare/build/Debug/lib/libguild_service_welfare.a
        ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildserviceauth/build/Debug/lib/libguild_service_auth.a
        ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildservicetimer/build/Debug/lib/libguild_service_timer.a
        ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildserviceactivity/build/Debug/lib/libguild_service_activity.a
        ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildservicebuilding/build/Debug/lib/libguild_service_building.a
        ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildservicetask/build/Debug/lib/libguild_service_task.a
        ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildservicerank/build/Debug/lib/libguild_service_rank.a

        wldap32.lib
        ws2_32.lib
        psapi.lib
    )
else()
    message("LINUX")

    set(USR_LOCAL_LIB_DIR /usr/local/lib)

    # set_target_properties(guild_service_unittest PROPERTIES LINK_FLAGS_DEBUG "
    #     -Wl,--whole-archive:${PROJECT_SOURCE_DIR}/../../../../lib/guildbasic/libguildbasic.a
    #     -Wl,--whole-archive:${PROJECT_SOURCE_DIR}/../../../../lib/proxy/guildbasic/GuildServiceBasic/libGuildServiceBasic_proxy.a
    #     -Wl,--whole-archive:${PROJECT_SOURCE_DIR}/../../../../lib/stub/guildbasic/GuildServiceBasic/libGuildServiceBasic_stub.a

    #     -Wl,--whole-archive:${PROJECT_SOURCE_DIR}/../../../../lib/guildservicequery/libguildservicequery.a
    #     -Wl,--whole-archive:${PROJECT_SOURCE_DIR}/../../../../lib/proxy/guildservicequery/GuildServiceQuery/libGuildServiceQuery_proxy.a
    #     -Wl,--whole-archive:${PROJECT_SOURCE_DIR}/../../../../lib/stub/guildservicequery/GuildServiceQuery/libGuildServiceQuery_stub.a

    #     -Wl,--whole-archive:${PROJECT_SOURCE_DIR}/../../../../lib/guildwelfare/libguildwelfare.a
    #     -Wl,--whole-archive:${PROJECT_SOURCE_DIR}/../../../../lib/proxy/guildwelfare/GuildServiceWelfare/libGuildServiceWelfare_proxy.a
    #     -Wl,--whole-archive:${PROJECT_SOURCE_DIR}/../../../../lib/stub/guildwelfare/GuildServiceWelfare/libGuildServiceWelfare_stub.a

    #     -Wl,--whole-archive:${PROJECT_SOURCE_DIR}/../../../../lib/guildserviceauth/libguildserviceauth.a
    #     -Wl,--whole-archive:${PROJECT_SOURCE_DIR}/../../../../lib/proxy/guildserviceauth/GuildServiceAuth/libGuildServiceAuth_proxy.a
    #     -Wl,--whole-archive:${PROJECT_SOURCE_DIR}/../../../../lib/stub/guildserviceauth/GuildServiceAuth/libGuildServiceAuth_stub.a
    # ")

    # set_target_properties(guild_service_unittest PROPERTIES LINK_FLAGS_RELEASE "
    #     -Wl,--whole-archive:${PROJECT_SOURCE_DIR}/../../../../lib/guildbasic/libguildbasic.a
    #     -Wl,--whole-archive:${PROJECT_SOURCE_DIR}/../../../../lib/proxy/guildbasic/GuildServiceBasic/libGuildServiceBasic_proxy.a
    #     -Wl,--whole-archive:${PROJECT_SOURCE_DIR}/../../../../lib/stub/guildbasic/GuildServiceBasic/libGuildServiceBasic_stub.a

    #     -Wl,--whole-archive:${PROJECT_SOURCE_DIR}/../../../../lib/guildservicequery/libguildservicequery.a
    #     -Wl,--whole-archive:${PROJECT_SOURCE_DIR}/../../../../lib/proxy/guildservicequery/GuildServiceQuery/libGuildServiceQuery_proxy.a
    #     -Wl,--whole-archive:${PROJECT_SOURCE_DIR}/../../../../lib/stub/guildservicequery/GuildServiceQuery/libGuildServiceQuery_stub.a

    #     -Wl,--whole-archive:${PROJECT_SOURCE_DIR}/../../../../lib/guildwelfare/libguildwelfare.a
    #     -Wl,--whole-archive:${PROJECT_SOURCE_DIR}/../../../../lib/proxy/guildwelfare/GuildServiceWelfare/libGuildServiceWelfare_proxy.a
    #     -Wl,--whole-archive:${PROJECT_SOURCE_DIR}/../../../../lib/stub/guildwelfare/GuildServiceWelfare/libGuildServiceWelfare_stub.a

    #     -Wl,--whole-archive:${PROJECT_SOURCE_DIR}/../../../../lib/guildserviceauth/libguildserviceauth.a
    #     -Wl,--whole-archive:${PROJECT_SOURCE_DIR}/../../../../lib/proxy/guildserviceauth/GuildServiceAuth/libGuildServiceAuth_proxy.a
    #     -Wl,--whole-archive:${PROJECT_SOURCE_DIR}/../../../../lib/stub/guildserviceauth/GuildServiceAuth/libGuildServiceAuth_stub.a
    # ")

    target_link_libraries(guild_service_unittest
        ${PROJECT_SOURCE_DIR}/../../../../../lib/libservice_box.a
        ${PROJECT_SOURCE_DIR}/../../../../lib/root/librpc.a
        ${PROJECT_SOURCE_DIR}/../../../../thirdparty/lib/linux/libprotobuf.a
        ${USR_LOCAL_LIB_DIR}/libzookeeper.a
        ${USR_LOCAL_LIB_DIR}/libhashtable.a
        ${USR_LOCAL_LIB_DIR}/libhiredis.a
        ${USR_LOCAL_LIB_DIR}/libcurl.a
        ${USR_LOCAL_LIB_DIR}/libz.a
        debug ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildservicebasic/build/Debug/lib/libguild_service_basic.a optimized ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildservicebasic/build/Release/lib/libguild_service_basic.a
        debug ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildservicequery/build/Debug/lib/libguild_service_query.a optimized ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildservicequery/build/Release/lib/libguild_service_query.a
        debug ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildservicewelfare/build/Debug/lib/libguild_service_welfare.a optimized ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildservicewelfare/build/Release/lib/libguild_service_welfare.a
        debug ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildserviceauth/build/Debug/lib/libguild_service_auth.a optimized ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildserviceauth/build/Release/lib/libguild_service_auth.a
        debug ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildservicetimer/build/Debug/lib/libguild_service_timer.a optimized ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildserviceauth/build/Release/lib/libguild_service_timer.a
        debug ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildserviceactivity/build/Debug/lib/libguild_service_activity.a optimized ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildserviceauth/build/Release/lib/libguild_service_activity.a
        debug ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildservicetimer/build/Debug/lib/libguild_service_timer.a optimized ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildserviceauth/build/Release/lib/libguild_service_timer.a
        debug ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildservicetask/build/Debug/lib/libguild_service_task.a optimized ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildserviceauth/build/Release/lib/libguild_service_task.a
        debug ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildservicerank/build/Debug/lib/libguild_service_rank.a optimized ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildserviceauth/build/Release/lib/libguild_service_rank.a
        debug ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildservicebuilding/build/Debug/lib/libguild_service_building.a optimized ${PROJECT_SOURCE_DIR}/../../../../usr/guildservice/src/guildserviceauth/build/Release/lib/libguild_service_building.a
        -lpthread
        -ldl
        "$<$<BOOL:${GCOV}>:libgcov.a>"
    )

    set(COMPILER_FLAGS)
    set(COMPILER_FLAGS_DEBUG)
    set(COMPILER_FLAGS_RELEASE)
    if(CMAKE_CXX_COMPILER_ID MATCHES GNU)
        list(APPEND COMPILER_FLAGS "-fnon-call-exceptions" "-fPIC")
        list(APPEND COMPILER_FLAGS_DEBUG "-g" "-O0")
        list(APPEND COMPILER_FLAGS_RELEASE "-O2" "-Wall" "-Wunused-variable")
        set(CMAKE_CXX_COMPILER "g++")
    elseif(CMAKE_CXX_COMPILER_ID MATCHES Clang)
        list(APPEND COMPILER_FLAGS_FLAGS "-fnon-call-exceptions" "-fPIC")
        list(APPEND COMPILER_FLAGS_DEBUG "-g" "-O0")
        list(APPEND COMPILER_FLAGS_RELEASE "-O2" "-Wall" "-Wunused-variable")
        set(CMAKE_CXX_COMPILER "clang++")
    endif()

    target_compile_options(${PROJECT_NAME}
        PRIVATE
        ${COMPILER_FLAGS}
        $<$<CONFIG:Debug>:${COMPILER_FLAGS_DEBUG}>
        $<$<CONFIG:Release>:${COMPILER_FLAGS_RELEASE}>
    )

endif()
